package com.echat.serviceapigateway.security.controller.admin;

import com.echat.serviceapigateway.security.common.HttpResult;
import com.echat.serviceapigateway.security.common.LayUIResult;
import com.echat.serviceapigateway.security.constant.Constants;
import com.echat.serviceapigateway.security.model.admin.AdminUser;
import com.echat.serviceapigateway.security.model.authentication.User;
import com.echat.serviceapigateway.security.model.authorization.vo.PermissionNode;
import com.echat.serviceapigateway.security.model.network.AccessIp;
import com.echat.serviceapigateway.security.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by chenmohan on 2018/09/14
 *
 * @description
 */
@RestController
@RequestMapping(value = "/admin",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class AdminUserController {

    @Autowired
    AdminUserService adminUserService;

    @Autowired
    UserService userService;

    @Autowired
    ResourceService resourceService;

    @Autowired
    AuthorizationService authorizationService;

    @Autowired
    IpWhiteListService ipWhiteListService;

    @RequestMapping("/hack")
    public HttpResult<Void> hackLogin(HttpSession session) {
        AdminUser hackUser = AdminUser.builder()
                .userName("admin")
                .password("admin")
                .build();
        AdminUser login = adminUserService.login(hackUser);
        session.setAttribute("userName", login.getUserName());
        return HttpResult.successWithMsg("use account[admin]");
    }

    @RequestMapping("/login")
    public HttpResult<AdminUser> login(AdminUser adminUser, HttpSession session) {
        AdminUser loginUser = adminUserService.login(adminUser);
        session.setAttribute("userName", loginUser.getUserName());
        return HttpResult.success(loginUser);
    }

    @RequestMapping("/logout")
    public HttpResult<AdminUser> logout(AdminUser adminUser, HttpSession session) {
        session.removeAttribute("userName");
        return HttpResult.success();
    }

    /**
     * 激活用户状态
     *
     * @param userId
     * @return
     */
    @RequestMapping("/active")
    public HttpResult<Void> activeUser(Long userId) {
        userService.activeUser(userId);
        return HttpResult.success();
    }

    /**
     * 管理员创建用户，创建用户的用户状态默认已激活
     *
     * @param user
     * @return
     */
    @RequestMapping("/createUser")
    public HttpResult<User> createUser(User user) {
        user.setStatus(Constants.USER_ENABLE);
        User createUser = userService.createUser(user);
        return HttpResult.success(createUser);
    }

    /**
     * 展示所有用户
     *
     * @param condition
     * @return
     */
    @RequestMapping("/listUsers")
    public LayUIResult listUsers(User condition) {
        return LayUIResult.withPageInfo(userService.listUsersByCondition(condition));
    }

    /**
     * 获取指定用户的权限树
     *
     * @param userId 指定用户的userId
     * @return
     */
    @RequestMapping("/authtree")
    public HttpResult<List<PermissionNode>> listUserPermissions(Long userId) {
        return HttpResult.success(resourceService.getAuthTree(userId));
    }

    /**
     * 更新用户的权限树
     *
     * @param userId
     * @param authIds
     * @return
     */
    @RequestMapping("/modifyAuth")
    public HttpResult<List> modfiyUserPermissions(Long userId, String authIds) {
        authorizationService.updateAuthCodeByUserId(userId, authIds);
        return HttpResult.success();
    }

    /**
     * 更新(刷新)用户的一些基本信息(包括ip白名单)
     *
     * @param user
     * @return
     */
    @RequestMapping("/refresh")
    public HttpResult<User> refreshUser(User user) {
        return HttpResult.success(userService.refresh(user));
    }

    /**
     * 获取指定用户id的白名单列表
     * @param userId
     * @return
     */
    @RequestMapping("/getIPList")
    public HttpResult<AccessIp> getIPWhiteList(Long userId) {
        AccessIp ip = ipWhiteListService.getAccessIpByUserId(userId);
        return HttpResult.success(ip);
    }
}
